package com.rede.didiok.admin.service.impl;

import java.util.*;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rede.didiok.admin.dao.WebConfigDao;
import com.rede.didiok.admin.entity.SystemConfigEntity;
import com.rede.didiok.admin.entity.WebConfigEntity;
import com.rede.didiok.admin.service.SystemConfigService;
import com.rede.didiok.admin.service.WebConfigService;
import com.rede.didiok.admin.utils.FileUtil;
import com.rede.didiok.common.constant.conf.MessageConf;
import com.rede.didiok.common.constant.conf.RedisConf;
import com.rede.didiok.common.constant.conf.SQLConf;
import com.rede.didiok.common.constant.conf.SysConf;
import com.rede.didiok.common.constant.global.Constants;
import com.rede.didiok.common.constant.global.ErrorCode;
import com.rede.didiok.common.dto.admin.SystemConfigDto;
import com.rede.didiok.common.dto.admin.WebConfigDto;
import com.rede.didiok.common.enums.*;
import com.rede.didiok.common.exception.exceptionType.QueryException;
import com.rede.didiok.common.feign.FileFeignClient;
import com.rede.didiok.common.schema.ComponentShowMap;
import com.rede.didiok.common.utils.JsonUtils;
import com.rede.didiok.common.utils.RedisUtil;
import com.rede.didiok.common.utils.ResultUtil;
import com.rede.didiok.common.utils.StringUtils;
import com.rede.didiok.common.utils.business.WebUtil;

@Slf4j
@Service("webConfigService")
public class WebConfigServiceImpl extends ServiceImpl<WebConfigDao, WebConfigEntity> implements WebConfigService {


    @Autowired
    private FileUtil fileUtil;

    @Autowired
    private WebConfigService webConfigService;

    @Autowired
    private WebConfigDao webConfigDao;

    @Autowired
    private SystemConfigService systemConfigService;

    @Resource
    private FileFeignClient fileFeignClient;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private WebUtil webUtil;


    /***************************** admin:start **************************/
    // region  didiok-admin

    /**
     * 获取网站名称
     *
     * @return
     */
    @Override
    public WebConfigEntity getWebSiteInfo() {
        QueryWrapper<WebConfigEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.last(SysConf.LIMIT_ONE);
        WebConfigEntity webConfig = webConfigService.getOne(queryWrapper);
        WebConfigEntity resultWebConfig = new WebConfigEntity();
        if (webConfig != null) {
            resultWebConfig.setTitle(webConfig.getTitle());
            resultWebConfig.setSummary(webConfig.getSummary());
            resultWebConfig.setKeyword(webConfig.getKeyword());
            resultWebConfig.setName(webConfig.getName());
            //获取图片
            if (webConfig != null && StringUtils.isNotEmpty(webConfig.getLogo())) {
                String pictureList = fileFeignClient.getPicture(webConfig.getLogo(), SysConf.FILE_SEGMENTATION);
                resultWebConfig.setLogo(webConfig.getLogo());

                List<String> photoList = fileUtil.getPicture(pictureList);
                if (photoList.size() > 0) {
                    resultWebConfig.setLogoPhoto(photoList.get(0));
                }
            }
        }
        return resultWebConfig;
    }

    /**
     * @Deprecated
     * 此方法弃用
     * @return
     */
    @Override
    public WebConfigEntity getWebConfig() {
        //从Redis中获取IP来源
        String webConfigResult = redisUtil.get(RedisConf.WEB_CONFIG);
        if (StringUtils.isNotEmpty(webConfigResult)) {
            WebConfigEntity webConfig = JsonUtils.jsonToPojo(webConfigResult, WebConfigEntity.class);
            return webConfig;
        }

        QueryWrapper<WebConfigEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc(SQLConf.CREATE_TIME);
        queryWrapper.last(SysConf.LIMIT_ONE);
        WebConfigEntity webConfig = webConfigService.getOne(queryWrapper);
        setWebConfigPhoto(webConfig);

        // 将WebConfig存到Redis中 [过期时间24小时*30天]
        redisUtil.setEx(RedisConf.WEB_CONFIG, JsonUtils.objectToJson(webConfig), 720, TimeUnit.HOURS);
        // 在redis中存储 WEB_CONFIG:OPEN_MOBILE_COMMENT
        String redisKey = RedisConf.WEB_CONFIG + RedisConf.SEGMENTATION + RedisConf.OPEN_MOBILE_COMMENT;
        String paramsValue = webConfig.getOpenMobileComment().toString();
        redisUtil.set(redisKey, paramsValue);

        return webConfig;
    }

    @Override
    public String editWebConfig(WebConfigDto webConfigDto) {
        if (StringUtils.isEmpty(webConfigDto.getUid())) {
            WebConfigEntity webConfig = new WebConfigEntity();
            // 设置网站配置【使用Spring工具类提供的深拷贝】
            BeanUtils.copyProperties(webConfigDto, webConfig, SysConf.STATUS);
            webConfigService.save(webConfig);

        } else {
            WebConfigEntity webConfig = webConfigService.getById(webConfigDto.getUid());
            // 更新网站配置【使用Spring工具类提供的深拷贝】
            BeanUtils.copyProperties(webConfigDto, webConfig, SysConf.STATUS, SysConf.UID);
            webConfig.setUpdateTime(new Date());
            webConfigService.updateById(webConfig);
        }

        // 修改配置后，清空Redis中的 WEB_CONFIG
        Set<String> webConfigKeySet = redisUtil.keys(RedisConf.WEB_CONFIG + Constants.SYMBOL_STAR);
        redisUtil.delete(webConfigKeySet);

        // 同时清空Redis中的登录方式
        Set<String> keySet = redisUtil.keys(RedisConf.LOGIN_TYPE + Constants.SYMBOL_STAR);
        redisUtil.delete(keySet);

        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    // endregion
    /***************************** admin:end **************************/



    /***************************** web:start **************************/
    // region didiok-web

    @Override
    public WebConfigEntity getWebConfigByShowList() {
        //从Redis中获取IP来源
        String webConfigResult = redisUtil.get(RedisConf.WEB_CONFIG);
        if (StringUtils.isNotEmpty(webConfigResult)) {
            WebConfigEntity webConfig = JsonUtils.jsonToPojo(webConfigResult, WebConfigEntity.class);
            return webConfig;
        }

        QueryWrapper<WebConfigEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SysConf.STATUS, StatusEnum.ENABLE);
        queryWrapper.orderByDesc(SQLConf.CREATE_TIME);
        WebConfigEntity webConfig = webConfigService.getOne(queryWrapper);
        if (webConfig == null) {
            throw new QueryException(ErrorCode.SYSTEM_CONFIG_IS_NOT_EXIST, MessageConf.SYSTEM_CONFIG_IS_NOT_EXIST);
        }
        // 设置配置
        setWebConfigPhoto(webConfig);

        // 过滤一些不需要显示的用户账号信息
        String showListJson = webConfig.getShowList();

        // 获取联系方式
        String email = webConfig.getEmail();
        String qqNumber = webConfig.getQqNumber();
        String qqGroup = webConfig.getQqGroup();
        String github = webConfig.getGithub();
        String gitee = webConfig.getGitee();
        String weChat = webConfig.getWeChat();

        // 将联系方式全部置空
        webConfig.setEmail("");
        webConfig.setQqNumber("");
        webConfig.setQqGroup("");
        webConfig.setGithub("");
        webConfig.setGitee("");
        webConfig.setWeChat("");

        // 判断哪些联系方式需要显示出来
        List<String> showList = JsonUtils.jsonToList(showListJson, String.class);
        for (String item : showList) {
            if (AccountTypeEnum.EMail.getCode().equals(item)) {
                webConfig.setEmail(email);
            }
            if (AccountTypeEnum.QQNumber.getCode().equals(item)) {
                webConfig.setQqNumber(qqNumber);
            }
            if (AccountTypeEnum.QQGroup.getCode().equals(item)) {
                webConfig.setQqGroup(qqGroup);
            }
            if (AccountTypeEnum.Github.getCode().equals(item)) {
                webConfig.setGithub(github);
            }
            if (AccountTypeEnum.Gitee.getCode().equals(item)) {
                webConfig.setGitee(gitee);
            }
            if (AccountTypeEnum.WeChat.getCode().equals(item)) {
                webConfig.setWeChat(weChat);
            }
        }

        ComponentShowMap componentShowMap = new ComponentShowMap();
        if (StringUtils.isNotEmpty(webConfig.getComponentShowList())) {
            List<String> componentShowList = JsonUtils.jsonToList(webConfig.getComponentShowList(), String.class);
            if (componentShowList != null) {
                for (String item : componentShowList) {
                    EComponentType eComponentType = EComponentType.getComponentType(item);
                    if (eComponentType == null) continue;
                    switch (eComponentType) {
                        case BlogSort: {
                            componentShowMap.setShowBlogSort(true);
                        } break;
                        case UserCard: {
                            componentShowMap.setShowUserCard(true);
                        } break;
                        case UserRank: {
                            componentShowMap.setShowUserRank(true);
                        } break;
                        case HotClick: {
                            componentShowMap.setShowHotClick(true);
                        } break;
                        case FollowUs: {
                            componentShowMap.setShowFollowUs(true);
                        } break;
                        case ThreeRecommend: {
                            componentShowMap.setShowThreeRecommend(true);
                        } break;
                        case HotTag: {
                            componentShowMap.setShowHotTag(true);
                        } break;
                        case UserComment: {
                            componentShowMap.setShowUserComment(true);
                        } break;
                        case UserTask: {
                            componentShowMap.setShowUserTask(true);
                        } break;
                        case FirstLevel: {
                            componentShowMap.setShowFirstLevel(true);
                        } break;
                        case ChatRoom: {
                            componentShowMap.setShowChatRoom(true);
                        } break;
                        case BlackModel: {
                            componentShowMap.setShowBlackModel(true);
                        } break;
                        case UserSign: {
                            componentShowMap.setShowUserSign(true);
                        } break;
                        case FourRecommend: {
                            componentShowMap.setShowFourRecommend(true);
                        } break;
                        case UserLink: {
                            componentShowMap.setShowUserLink(true);
                        } break;
                        case UserNotice: {
                            componentShowMap.setShowUserNotice(true);
                        } break;
                        case UploadArticle: {
                            componentShowMap.setShowUploadArticle(true);
                        } break;
                        case CreateMoment: {
                            componentShowMap.setShowCreateMoment(true);
                        } break;
                        case CreateProblem: {
                            componentShowMap.setShowCreateProblem(true);
                        } break;
                        case CreateQuestion: {
                            componentShowMap.setShowCreateQuestion(true);
                        } break;
                        case CreateArticle: {
                            componentShowMap.setShowCreateArticle(true);
                        } break;
                        case UserSearch: {
                            componentShowMap.setShowUserSearch(true);
                        } break;
                        case UserCollect: {
                            componentShowMap.setShowUserCollect(true);
                        } break;
                        case CreateButton: {
                            componentShowMap.setShowCreateButton(true);
                        } break;
                        case UserPrise: {
                            componentShowMap.setShowUserPrise(true);
                        } break;
                        case UserTread: {
                            componentShowMap.setShowUserTread(true);
                        } break;
                        case DeleteComment: {
                            componentShowMap.setShowDeleteComment(true);
                        } break;
                        case UserReport: {
                            componentShowMap.setShowUserReport(true);
                        } break;
                        case GeneralEdit: {
                            componentShowMap.setShowGeneralEdit(true);
                        } break;
                        case VipModel: {
                            componentShowMap.setShowVipModel(true);
                        } break;
                    }
                }
            }
        } else {
            componentShowMap.setShowBlogSort(true);
            componentShowMap.setShowUserCard(true);
            componentShowMap.setShowUserRank(true);
            componentShowMap.setShowHotClick(true);
            componentShowMap.setShowFollowUs(true);
            componentShowMap.setShowThreeRecommend(true);
            componentShowMap.setShowHotTag(true);
            componentShowMap.setShowUserComment(true);
            componentShowMap.setShowUserTask(true);
            componentShowMap.setShowFirstLevel(true);
            componentShowMap.setShowChatRoom(true);
            componentShowMap.setShowBlackModel(true);
            componentShowMap.setShowUserSign(true);
            componentShowMap.setShowFourRecommend(true);
            componentShowMap.setShowUserLink(true);
            componentShowMap.setShowUserNotice(true);
            componentShowMap.setShowUploadArticle(true);
            componentShowMap.setShowCreateMoment(true);
            componentShowMap.setShowCreateProblem(true);
            componentShowMap.setShowCreateQuestion(true);
            componentShowMap.setShowCreateArticle(true);
            componentShowMap.setShowUserSearch(true);
            componentShowMap.setShowCreateButton(true);
            componentShowMap.setShowUserCollect(true);
            componentShowMap.setShowUserPrise(true);
            componentShowMap.setShowUserTread(true);
            componentShowMap.setShowDeleteComment(true);
            componentShowMap.setShowUserReport(true);
            componentShowMap.setShowGeneralEdit(true);
            componentShowMap.setShowVipModel(true);
        }
        webConfig.setComponentShowMap(componentShowMap);

        // 设置聊天室功能是否开启
        redisUtil.setEx(RedisConf.WEB_CONFIG + Constants.SYMBOL_COLON + RedisConf.OPEN_EMOJI, JsonUtils.objectToJson(0), 720, TimeUnit.HOURS);
        redisUtil.setEx(RedisConf.WEB_CONFIG + Constants.SYMBOL_COLON + RedisConf.OPEN_RECORD, JsonUtils.objectToJson(0), 720, TimeUnit.HOURS);
        redisUtil.setEx(RedisConf.WEB_CONFIG + Constants.SYMBOL_COLON + RedisConf.OPEN_PICTURE, JsonUtils.objectToJson(0), 720, TimeUnit.HOURS);
        redisUtil.setEx(RedisConf.WEB_CONFIG + Constants.SYMBOL_COLON + RedisConf.OPEN_CALL, JsonUtils.objectToJson(0), 720, TimeUnit.HOURS);
        redisUtil.setEx(RedisConf.WEB_CONFIG + Constants.SYMBOL_COLON + RedisConf.OPEN_VEDIO, JsonUtils.objectToJson(0), 720, TimeUnit.HOURS);
        List<String> showChatTypeList = JsonUtils.jsonToList(webConfig.getChatTypeList(), String.class);
        for (String item : showChatTypeList) {
            if (ChatTypeEnum.Emoji.getCode().equals(item)) {
                redisUtil.setEx(RedisConf.WEB_CONFIG + Constants.SYMBOL_COLON + RedisConf.OPEN_EMOJI, JsonUtils.objectToJson(1), 720, TimeUnit.HOURS);
            } else if (ChatTypeEnum.Record.getCode().equals(item)) {
                redisUtil.setEx(RedisConf.WEB_CONFIG + Constants.SYMBOL_COLON + RedisConf.OPEN_RECORD, JsonUtils.objectToJson(1), 720, TimeUnit.HOURS);
            } else if (ChatTypeEnum.Picture.getCode().equals(item)) {
                redisUtil.setEx(RedisConf.WEB_CONFIG + Constants.SYMBOL_COLON + RedisConf.OPEN_PICTURE, JsonUtils.objectToJson(1), 720, TimeUnit.HOURS);
            } else if (ChatTypeEnum.Call.getCode().equals(item)) {
                redisUtil.setEx(RedisConf.WEB_CONFIG + Constants.SYMBOL_COLON + RedisConf.OPEN_CALL, JsonUtils.objectToJson(1), 720, TimeUnit.HOURS);
            } else if (ChatTypeEnum.Vedio.getCode().equals(item)) {
                redisUtil.setEx(RedisConf.WEB_CONFIG + Constants.SYMBOL_COLON + RedisConf.OPEN_VEDIO, JsonUtils.objectToJson(1), 720, TimeUnit.HOURS);
            }
        }
        // 设置聊天室是否开启
        redisUtil.setEx(RedisConf.WEB_CONFIG + Constants.SYMBOL_COLON + RedisConf.OPEN_CHAT, JsonUtils.objectToJson(Integer.valueOf(webConfig.getOpenChat())), 720, TimeUnit.HOURS);

        // 将WebConfig存到Redis中 [过期时间24小时*30天]
        redisUtil.setEx(RedisConf.WEB_CONFIG, JsonUtils.objectToJson(webConfig), 720, TimeUnit.HOURS);

        // 调用一次获取功能，刷新redis配置
        this.getWebConfigByShowList();

        return webConfig;
    }

    @Override
    public String getOpenMobileComment() {
        // 判断Redis中是否包含该key的数据
        String redisKey = RedisConf.WEB_CONFIG + RedisConf.SEGMENTATION + RedisConf.OPEN_MOBILE_COMMENT;
        String paramsValue = redisUtil.get(redisKey);
        // 如果Redis中不存在，那么从数据库中获取
        if (StringUtils.isEmpty(paramsValue)) {
            WebConfigEntity webConfig = webConfigDao.getOpenMobileComment();
            if (webConfig == null) {
                throw new QueryException(ErrorCode.SYSTEM_CONFIG_IS_NOT_EXIST, MessageConf.SYSTEM_CONFIG_IS_NOT_EXIST);
            }
            log.info("从库中获取OpenMobileComment：{}", webConfig.getOpenMobileComment());
            // 如果数据库也不存在，将抛出异常【需要到找到 doc/数据库脚本 更新数据库中的 t_sys_params表】
            if (webConfig == null || webConfig.getOpenMobileComment() == null) {
                String errInfo = String.format("数据库a_web_config表，缺少配置文件：%s", RedisConf.OPEN_MOBILE_COMMENT);
                log.error("[getOpenMobileComment] {}", errInfo);
                throw new QueryException(ErrorCode.PLEASE_CONFIGURE_SYSTEM_PARAMS, errInfo);
            }
            paramsValue = webConfig.getOpenMobileComment().toString();
            redisUtil.set(redisKey, paramsValue);
        }

        return paramsValue;
    }

    /**
     * 是否开启该登录方式【账号密码、码云、Github、QQ、微信】
     *
     * @param loginType
     * @return
     */
    @Override
    public Boolean isOpenLoginType(String loginType) {
        String loginTypeJson = redisUtil.get(RedisConf.LOGIN_TYPE + Constants.SYMBOL_COLON + loginType);
        // 判断redis中是否包含该登录记录
        if (StringUtils.isNotEmpty(loginTypeJson)) {
            // 如果Redis中有内容，表示开启该登录方式
            return true;
        } else if (loginTypeJson != null && loginTypeJson.length() == 0) {
            // 如果内容为空串，表示没有开启该登录方式
            return false;
        }

        QueryWrapper<WebConfigEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SysConf.STATUS, StatusEnum.ENABLE);
        queryWrapper.orderByDesc(SQLConf.CREATE_TIME);
        WebConfigEntity webConfig = this.getOne(queryWrapper);
        if (webConfig == null) {
            throw new QueryException(ErrorCode.SYSTEM_CONFIG_IS_NOT_EXIST, MessageConf.SYSTEM_CONFIG_IS_NOT_EXIST);
        }
        // 过滤一些不需要显示的用户账号信息
        String loginTypeListJson = webConfig.getLoginTypeList();
        // 判断哪些联系方式需要显示出来
        List<String> loginTypeList = JsonUtils.jsonToList(loginTypeListJson, String.class);
        for (String item : loginTypeList) {
            if (LoginTypeEnum.PASSWORD.getCode().equals(item)) {
                redisUtil.set(RedisConf.LOGIN_TYPE + Constants.SYMBOL_COLON + RedisConf.PASSWORD, LoginTypeEnum.PASSWORD.getName());
            }
            if (LoginTypeEnum.GITEE.getCode().equals(item)) {
                redisUtil.set(RedisConf.LOGIN_TYPE + Constants.SYMBOL_COLON + RedisConf.GITEE, LoginTypeEnum.GITEE.getName());
            }
            if (LoginTypeEnum.GITHUB.getCode().equals(item)) {
                redisUtil.set(RedisConf.LOGIN_TYPE + Constants.SYMBOL_COLON + RedisConf.GITHUB, LoginTypeEnum.GITHUB.getName());
            }
            if (LoginTypeEnum.QQ.getCode().equals(item)) {
                redisUtil.set(RedisConf.LOGIN_TYPE + Constants.SYMBOL_COLON + RedisConf.QQ, LoginTypeEnum.QQ.getName());
            }
            if (LoginTypeEnum.WECHAT.getCode().equals(item)) {
                redisUtil.set(RedisConf.LOGIN_TYPE + Constants.SYMBOL_COLON + RedisConf.WECHAT, LoginTypeEnum.WECHAT.getName());
            }
        }
        // 再次判断该登录方式是否开启
        loginTypeJson = redisUtil.get(RedisConf.LOGIN_TYPE + Constants.SYMBOL_COLON + loginType);
        if (StringUtils.isNotEmpty(loginTypeJson)) {
            return true;
        } else {
            // 设置一个为空的字符串【防止缓存穿透】
            redisUtil.set(RedisConf.LOGIN_TYPE + Constants.SYMBOL_COLON + loginType, "");
            return false;
        }
    }

    // endregion
    /***************************** web:end **************************/

    private void setWebConfigPhoto(WebConfigEntity webConfig) {
        StringBuilder stringBuilder = new StringBuilder();
        String pictureResult = "";
        // 获取LOGO
        if (StringUtils.isNotEmpty(webConfig.getLogo())) {
            stringBuilder.append(webConfig.getLogo() + Constants.SYMBOL_COMMA);
        }
        if (StringUtils.isNotEmpty(webConfig.getAliPay())) {
            stringBuilder.append(webConfig.getAliPay() + Constants.SYMBOL_COMMA);
        }
        if (StringUtils.isNotEmpty(webConfig.getWeixinPay())) {
            stringBuilder.append(webConfig.getWeixinPay() + Constants.SYMBOL_COMMA);
        }
        if (StringUtils.isNotEmpty(webConfig.getLoadingValidFileUid())) {
            stringBuilder.append(webConfig.getLoadingValidFileUid() + Constants.SYMBOL_COMMA);
        }
        if (StringUtils.isNotEmpty(webConfig.getWechatFileUid())) {
            stringBuilder.append(webConfig.getWechatFileUid() + Constants.SYMBOL_COMMA);
        }
        if (StringUtils.isNotEmpty(webConfig.getMiniFileUid())) {
            stringBuilder.append(webConfig.getMiniFileUid() + Constants.SYMBOL_COMMA);
        }
        if (StringUtils.isNotEmpty(webConfig.getGroupFileUid())) {
            stringBuilder.append(webConfig.getGroupFileUid() + Constants.SYMBOL_COMMA);
        }

        if (stringBuilder != null) {
            pictureResult = fileFeignClient.getPicture(stringBuilder.toString(), Constants.SYMBOL_COMMA);
        }
        List<Map<String, Object>> pictureList = getPictureMap(pictureResult);
        Map<String, String> pictureMap = new HashMap<>();
        pictureList.forEach(item -> {
            pictureMap.put(item.get(SQLConf.UID).toString(), item.get(SQLConf.URL).toString());
        });

        // 获取LOGO
        if (StringUtils.isNotEmpty(webConfig.getLogo())) {
            List<String> logoList = StringUtils.changeStringToString(webConfig.getLogo(), com.rede.didiok.common.constant.global.Constants.SYMBOL_COMMA);
            if (logoList.size() > 0) {
                if (pictureMap.get(logoList.get(0)) != null) {
                    webConfig.setLogoPhoto(pictureMap.get(logoList.get(0)));
                }
            }
        }
        // 获取阿里支付码
        if (StringUtils.isNotEmpty(webConfig.getAliPay()) && pictureMap.get(webConfig.getAliPay()) != null) {
            webConfig.setAliPayPhoto(pictureMap.get(webConfig.getAliPay()));
        }
        // 获取微信支付码
        if (StringUtils.isNotEmpty(webConfig.getWeixinPay()) && pictureMap.get(webConfig.getWeixinPay()) != null) {
            webConfig.setWeixinPayPhoto(pictureMap.get(webConfig.getWeixinPay()));
        }
        // 获取加载校验码
        if (StringUtils.isNotEmpty(webConfig.getLoadingValidFileUid()) && pictureMap.get(webConfig.getLoadingValidFileUid()) != null) {
            webConfig.setLoadingValidPhoto(pictureMap.get(webConfig.getLoadingValidFileUid()));
        }
        // 获取微信公众号
        if (StringUtils.isNotEmpty(webConfig.getWechatFileUid()) && pictureMap.get(webConfig.getWechatFileUid()) != null) {
            webConfig.setWechatPhoto(pictureMap.get(webConfig.getWechatFileUid()));
        }
        // 获取小程序
        if (StringUtils.isNotEmpty(webConfig.getMiniFileUid()) && pictureMap.get(webConfig.getMiniFileUid()) != null) {
            webConfig.setMiniPhoto(pictureMap.get(webConfig.getMiniFileUid()));
        }
        // 获取微信群
        if (StringUtils.isNotEmpty(webConfig.getGroupFileUid()) && pictureMap.get(webConfig.getGroupFileUid()) != null) {
            webConfig.setGroupPhoto(pictureMap.get(webConfig.getGroupFileUid()));
        }
    }


    /**
     * 获取图片，返回Map
     *
     * @param result
     * @return
     */
    private List<Map<String, Object>> getPictureMap(String result) {

        String picturePriority = "";
        String localPictureBaseUrl = "";
        String qiNiuPictureBaseUrl = "";
        String minioPictureBaseUrl = "";
        String aliossPictureBaseUrl = "";
        // 从Redis中获取系统配置
        String systemConfigJson = redisUtil.get(RedisConf.SYSTEM_CONFIG);
        if (org.springframework.util.StringUtils.isEmpty(systemConfigJson)) {
            //  获取SystemConf
            SystemConfigEntity systemConfig = systemConfigService.getOneSystemConfig();

            picturePriority = systemConfig.getPicturePriority();
            localPictureBaseUrl = systemConfig.getLocalPictureBaseUrl();
            qiNiuPictureBaseUrl = systemConfig.getQiNiuPictureBaseUrl();
            minioPictureBaseUrl = systemConfig.getMinioPictureBaseUrl();
            aliossPictureBaseUrl = systemConfig.getAliossPictureBaseUrl();
        } else {
            SystemConfigDto systemConfig = JsonUtils.jsonToPojo(systemConfigJson, SystemConfigDto.class);
            picturePriority = systemConfig.getPicturePriority();
            localPictureBaseUrl = systemConfig.getLocalPictureBaseUrl();
            qiNiuPictureBaseUrl = systemConfig.getQiNiuPictureBaseUrl();
            minioPictureBaseUrl = systemConfig.getMinioPictureBaseUrl();
            aliossPictureBaseUrl = systemConfig.getAliossPictureBaseUrl();
        }

        List<Map<String, Object>> resultList = new ArrayList<>();
        Map<String, Object> picMap = (Map<String, Object>) JsonUtils.jsonToObject(result, Map.class);
        if (SysConf.SUCCESS.equals(picMap.get(SysConf.CODE))) {
            List<Map<String, Object>> picData = (List<Map<String, Object>>) picMap.get(SysConf.DATA);
            if (picData.size() > 0) {
                for (int i = 0; i < picData.size(); i++) {
                    Map<String, Object> map = new HashMap<>();
                    if (org.springframework.util.StringUtils.isEmpty(picData.get(i).get(SysConf.UID))) {
                        continue;
                    }
                    // 判断文件显示优先级【需要显示存储在哪里的图片】
                    if (FilePriorityEnum.QI_NIU.equals(picturePriority)) {
                        map.put(SysConf.URL, qiNiuPictureBaseUrl + picData.get(i).get(SysConf.QI_NIU_URL));
                    } else if (FilePriorityEnum.MINIO.equals(picturePriority)) {
                        map.put(SysConf.URL, minioPictureBaseUrl + picData.get(i).get(SysConf.MINIO_URL));
                    } else if (FilePriorityEnum.ALIOSS.equals(picturePriority)) {
                        map.put(SysConf.URL, aliossPictureBaseUrl + picData.get(i).get(SysConf.ALIOSS_URL));
                    } else {
                        map.put(SysConf.URL, localPictureBaseUrl + picData.get(i).get(SysConf.URL));
                    }

                    map.put(SysConf.UID, picData.get(i).get(SysConf.UID));
                    resultList.add(map);
                }
            }
        } else if (SysConf.ERROR.equals(picMap.get(SysConf.CODE))) {
            log.error("获取图片失败，图片服务出现异常：{}", picMap.get(SysConf.MESSAGE));
        } else {
            log.error("获取图片失败");
        }
        return resultList;
    }

    /**
     * 获取一条可用的webConfig
     *
     * @return
     */
    @Override
    public WebConfigEntity getEnableWebConfig() {
        return this.getOne(
                new QueryWrapper<WebConfigEntity>()
                        .eq(SysConf.STATUS, StatusEnum.ENABLE)
                );
    }
}